package plagiarism;

import io.ScannerTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;



public class IO {
	private static IO io;
	public static int num;

	private IO() {

	}

	public static IO getInstance() {
		if (io == null) {
			IO.io = new IO();
			return IO.io;
		}
		return IO.io;
	}

	private void inputname(String[] article_names, InputStreamReader converter){
		System.out.printf("Please input %d files accordingly\n", num);
		BufferedReader file_in = new BufferedReader(converter);
		
		for (int i = 0; i < num; i++) {
			article_names[i] = (i+1) + ".txt";
		}
	}
	
	public void input() throws IOException {
		// to do two step
		// 1. ask the user how many files to input
		System.out.println("Now is IO");
		System.out.println("How many files do you want to input");
		InputStreamReader converter = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(converter);
		String text = in.readLine();
		while (!isInteger(text)) {
			// the user need to input again
			System.out.println("Your input is invalid, please try again");
			text = in.readLine();
		}
		num = Integer.parseInt(text);

		// 2. ask the user to input the file names
		String[] article_names = new String[num];
		inputname(article_names, converter);
		
		BigController bigcontroller = new BigController(article_names);
		
//		FileManager filemanager = new FileManager(article_names);
//		
//		// 3. send the filenames to the next class
//		for (int i = 0; i < num; i++) {
//			System.out.println(article_names[i]);
//		}
//		
//		ArrayList<String[]> a1_list = new ArrayList<String[]>();
//		
//		// 4. run scanmanager
//		/*
//		ScanManager scanmanager = new ScanManager(filemanager);
//		scanmanager.scan(a1_list, article_names);
//		*/
//		ScannerTest scannertest = new ScannerTest();
//		for(int i =0 ;i < article_names.length; i ++)
//		{
//			scannertest.setfilename(article_names[i]);
//			scannertest.run();
//			a1_list.add(scannertest.getlist());
//			scannertest.clear();
//		}
//		
//		
//		//Run CheckManager
//		CheckManager checkmanager = new CheckManager();
//		checkmanager.operation(a1_list);
//		checkmanager.checking();
//		checkmanager.output_all();
		
		
	}

	public static boolean isInteger(String s) {
		try {
			Integer.parseInt(s);
		} catch (NumberFormatException e) {
			return false;
		}
		// only got here if we didn't return false
		return true;
	}


	public void output(int rate, char code)
	{

		// to do the output


		if(rate >= 20)
		{
			System.out.println("Article A and Acticle " + code + " is plagiarism!!!!!!!!!!!!!!!!!!");
			System.out.println("The similar rate is: " + rate );
			System.out.println("Please take appropriate action!!!!!!!!!!\n\n");
		}
		else
		{
			System.out.println("Article A and Acticle " + code + " is not plagiarism.");
			System.out.println("The similar rate is: " + rate + "\n\n");
		}

	}

}
